import json
from django.db import models
# Create your model here.
from django.contrib.auth.models import User





#产品
class Product(models.Model):
    name = models.CharField(max_length=32)#产品名称
    date_added = models.DateTimeField(auto_now=True) #添加时间
    owner = models.ForeignKey(User,on_delete=models.CASCADE,related_name='owner_user') #产品经理
    member = models.ManyToManyField(User,related_name='member_user') #产品开发人员
    member_leader= models.ForeignKey(User,on_delete=models.CASCADE,related_name='member_leader_user')#开发经理
    participant = models.ManyToManyField(User,related_name='participant_user') #产品测试人员
    participant_leader = models.ForeignKey(User,on_delete=models.CASCADE,related_name='participant_leader_user') #产品测试负责人
    description = models.CharField(max_length=128,blank=True)#产品描述

    def __str__(self):
        return self.name

    def member_list(self):
        return ','.join([i.username for i in self.member.all()])

    def paticipant_list(self):
        return ','.join([i.username for i in self.participant.all()])


#Job
class Job(models.Model):
    name = models.CharField(max_length=30) #Job名称
    description = models.CharField(max_length=200) #Job描述
    product = models.ForeignKey(Product,on_delete=models.CASCADE,related_name='job_product')
    creat_time = models.DateTimeField(auto_now_add=True)
    last_run_time = models.DateTimeField(null=True)
    last_update_time = models.DateTimeField(null=True)
    last_run_result = models.CharField(max_length=10)

    def __str__(self):
        return self.name



class Suite(models.Model):
    name = models.CharField(max_length=30)
    description = models.CharField(max_length=200)
    job = models.ForeignKey(Job,on_delete=models.CASCADE,related_name='suite_job')
    creater = models.ForeignKey(User,on_delete=models.CASCADE,related_name='suite_creater')
    create_time = models.DateTimeField(auto_now_add=True)
    type = models.CharField(max_length=30,default='api')#套件类型

    def __str__(self):
        return self.name



class Step(models.Model):
    run_order = models.IntegerField() #运行顺序
    OPERATIONS = (
        ('OpenBrowser','打开浏览器'),
        ('Click','点击'),
        ('Type','输入'),
        ('Check','检查'),
        ('CloseBrowser','关闭浏览器'),
    )
    LOCATEMETHODS = (
        ('XPATH','XPATH'),
        ('ID','ID'),
        ('NAME','NAME'),
    )
    REQUESTMETHODS = (
        ('GET','GET'),
        ('POST','POST'),
    )
    RESULTS = (
        ('PASS','通过'),
        ('FAIL','失败'),
    )

    api_url = models.CharField(max_length=1024,null=True) #接口地址
    api_json = models.CharField(max_length=1024,null=True) #接口请求json
    api_method = models.CharField(max_length=20,choices=REQUESTMETHODS,default='GET',null=True) #接口请求方式
    api_header = models.CharField(max_length=1024,null=True)#接口请求头
    api_params = models.CharField(max_length=2048,null=True)#接口请求参数
    api_expect = models.CharField(max_length=2048,null=True)#接口预期返回值

    ui_operation = models.CharField(max_length=20,choices=OPERATIONS,default='OpenBrowser',null=True)  # 操作名称
    ui_locate_method = models.CharField(max_length=30,choices=LOCATEMETHODS,default='XPATH',null=True) #操作元素定位方式
    ui_locate_url = models.CharField(max_length=256,null=True) #操作元素定位表达式
    ui_data = models.CharField(max_length=1024,null=True)#元素操作的数据，可能是传入的数据可能是用来比对的数据

    result = models.CharField(max_length=4,choices=RESULTS,null=True)#步骤运行结果
    suite = models.ForeignKey(Suite,on_delete=models.CASCADE,related_name='step_suite')

